package semspe;

import java.sql.*;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class TraitRequete {
	
    JTable tabResultat = new JTable();
	
  public String [][] ExecuteRequete (String requeteSQL, String[] resultatVoulu, int nbResultat) {
    Connection con = null;
    Statement instruction = null;
    ResultSet resultat = null;
    
    
    
    String [][] res = new String [resultatVoulu.length][nbResultat]; 
    
    int i=0;
    try {
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      con = DriverManager.getConnection("jdbc:mysql://localhost/o112831_stock","root", "");

      if(!con.isClosed())
        System.out.println("Connexion ok");
      
      
      instruction = con.createStatement();
      resultat = instruction.executeQuery(requeteSQL);
      //resultat.next();
      for(i=0; i<resultatVoulu.length;i++)
      {
    	  int j=0;
    	  while(resultat.next())
    	  {
	    	  try {
	    		  res[i][j]=resultat.getString(resultatVoulu[i]);
	    		  j++;
	    	  }
	    	  catch(Exception e) {System.out.println("Erreur !!!!");}
    	  }
      }

	    } catch(Exception e) {
	      System.err.println("Exception: " + e.getMessage());
	    } finally {
	      try {
	        if(con != null)
	          con.close();
	      } catch(SQLException e) {} 
    	
    	
    	
    }
	return res;
    
    
    
  }
  
  public void ExecuteRequeteData (String requeteSQL) {
    Connection con = null;
    Statement instruction = null;

    int i=0;
    try {
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      con = DriverManager.getConnection("jdbc:mysql://localhost/o112831_stock","root", "");

      if(!con.isClosed())
        System.out.println("Connexion ok");
      
      
      instruction = con.createStatement();
      instruction.executeUpdate(requeteSQL);
      
      
	    } catch(Exception e) {
	      System.err.println("Exception: " + e.getMessage());
	    } finally {
	      try {
	        if(con != null)
	          con.close();
	      } catch(SQLException e) {} 
	
    } 
  }
  /*
  public static void main (String [] args)
  {
	  ConnectionBD connection = new ConnectionBD();
	  String [] resVoulu = new String []{"volAlcool"};
	  String[][] res = connection.ExecuteRequete("SELECT * from bouteille",resVoulu);
	  JOptionPane.showMessageDialog(null, res);
	  
	  
  }
  */
}